package com.fernfx.xingtan.common;

import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;

import com.fernfx.xingtan.Constant;
import com.fernfx.xingtan.ConsumerApplication;
import com.fernfx.xingtan.common.base.BaseActivity;
import com.fernfx.xingtan.common.entity.user.MemberEntity;
import com.fernfx.xingtan.main.ui.MainActivity;
import com.fernfx.xingtan.user.AccountManager;
import com.fernfx.xingtan.utils.LogUtils;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.ToastUtil;

import io.rong.imkit.RongIM;
import io.rong.imkit.fragment.ConversationListFragment;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.UserInfo;

import static io.rong.imlib.statistics.UserData.name;

/**
 * Created by admin on 2018/8/6.
 */

public final class RongYun {
    private RongYun(){}

    public static void connectRongYun(final BaseActivity baseActivity,final MemberEntity memberEntity, String token,final boolean flag) {
        if (baseActivity.getApplicationInfo().packageName.equals(OtherUtil.getCurProcessName(baseActivity.getApplicationContext()))) {

            RongIM.connect(token, new RongIMClient.ConnectCallback() {

                /**
                 * Token 错误。可以从下面两点检查 1.  Token 是否过期，如果过期您需要向 App Server 重新请求一个新的 Token
                 *                  2.  token 对应的 appKey 和工程里设置的 appKey 是否一致
                 */
                @Override
                public void onTokenIncorrect() {
                    ToastUtil.showCentertoast("融云Token过期或无效");
                }

                /**
                 * 连接融云成功
                 * @param userid 当前 token 对应的用户 id
                 */
                @Override
                public void onSuccess(String userid) {
                    LogUtils.d("LoginActivity", "--onSuccess" + userid);

                    //判断是什么环境测试环境加默认头像
                    setRongYunHeadImg(userid, memberEntity.getObj().getNickname(), memberEntity.getObj().getHeadImg());

                    //保存登录状态
                    if (flag) {
                        AccountManager.saveUserInfo(memberEntity);
                        AccountManager.saveLoginStatus(true);
                        //跳转到首页
                        MainActivity.start(baseActivity);
                        baseActivity.finish();
                    }
                }

                /**
                 * 连接融云失败
                 * @param errorCode 错误码，可到官网 查看错误码对应的注释
                 */
                @Override
                public void onError(RongIMClient.ErrorCode errorCode) {
                    String msg = errorCode.getMessage();
                    if (!TextUtils.isEmpty(msg)) {
                        ToastUtil.showCentertoast(msg);
                    }
                }
            });
        }
    }

    public static void setRongYunHeadImg(String userId,String userName,String userImg){
        if (TextUtils.isEmpty(userName)) {
            userName = userId;
        }
        if (TextUtils.isEmpty(userImg)) {
            userImg = "";
        }
        //连接成功给融云设置头像
        RongIM.getInstance().setCurrentUserInfo(new UserInfo(userId,userName, Uri.parse(userImg)));
        //刷新头像
        RongIM.getInstance().refreshUserInfoCache(new UserInfo(userId, userName, Uri.parse(userImg)));
    }

}
